package com.xhwl.centiotdata.suppliersystem.leopen;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.LeopenConstant;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.common.pojo.cent.iot.OrgInfo;
import com.xhwl.common.pojo.cent.sdata.Organization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组织
 *
 * @author pengjunming
 * @description
 * @date 2023/2/11 14:41
 */
@Service
@Slf4j
public class RegionLeopenHandler {

    @Resource
    private GeneralLeopenHandler generalLeopenHandler;

    /**
     * 批量创建组织
     *
     * @param config        配置
     * @param organization 组织列表
     * @return 组织列表
     */
    public boolean createOrg(PedestrianIntegrationConfigDTO config, Organization organization) {
        try {
            List<Map<String, Object>> content = new ArrayList<>();
            Map<String, Object> org = new HashMap<>();
            org.put("orgNum", organization.getId());
            org.put("orgName", organization.getName());
            org.put("orgLevel", organization.getLevel());
            org.put("superOrgNum", organization.getLevel() == 1 ? "" : organization.getParentId());
            content.add(org);
            JSONObject jsonData = generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ORG_ADD, content);
            log.info("同步组织成功:{}", organization.getId());

            //新增失败数据
            JSONArray errorList = jsonData.getJSONArray("errorList");
            if (!CollectionUtils.isEmpty(errorList)) {
                log.error("同步组织失败:{}", errorList);
                return false;
            }
        } catch (Exception e) {
            log.error("新增组织异常", e);
            return false;
        }
        return true;
    }

    public void deleteOrg(PedestrianIntegrationConfigDTO config, List<OrgInfo> infoList) {
        try {
            infoList.forEach(orgInfo -> {
                Map<String, Object> content = new HashMap<>();
                content.put("orgNum", orgInfo.getId());
                generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                        config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ORG_DELETE, content);
            });
        } catch (Exception e) {
            log.error("删除组织异常", e);
        }
    }

    public void updateOrg(PedestrianIntegrationConfigDTO config, OrgInfo updateOrg) {
        try {
            Map<String, Object> content = new HashMap<>();
            content.put("orgNum", updateOrg.getId());
            content.put("orgName", updateOrg.getOrgName());
            generalLeopenHandler.onPosting(config.getAk(), config.getSk(),
                    config.getPassword(), config.getIpAndPort(), LeopenConstant.LENIU_ORG_UPDATE, content);
        } catch (Exception e) {
            log.error("修改组织异常", e);
        }
    }
}
